package com.eshop.modules.mp.handler;

import com.eshop.modules.mp.builder.TextBuilder;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.builder.outxml.TransferCustomerServiceBuilder;
import org.apache.commons.lang3.StringUtils;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import java.util.Map;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import org.springframework.stereotype.Component;

@Component
public class MsgHandler extends AbstractHandler
{
    public WxMpXmlOutMessage handle(final WxMpXmlMessage wxMessage, final Map<String, Object> context, final WxMpService weixinService, final WxSessionManager sessionManager) {
        while (true) {
            if (!wxMessage.getMsgType().equals("event")) {
                try {
                    if (StringUtils.startsWithAny((CharSequence)wxMessage.getContent(), new CharSequence[] { "你好", "客服" }) && weixinService.getKefuService().kfOnlineList().getKfOnlineList().size() > 0) {
                        return (WxMpXmlOutMessage)((TransferCustomerServiceBuilder)((TransferCustomerServiceBuilder)WxMpXmlOutMessage.TRANSFER_CUSTOMER_SERVICE().fromUser(wxMessage.getToUser())).toUser(wxMessage.getFromUser())).build();
                    }
                }
                catch (WxErrorException e) {
                    e.printStackTrace();
                }
                final String content = "yshop收到信息内容：" + wxMessage.getContent();
                return new TextBuilder().build(content, wxMessage, weixinService);
            }
            continue;
        }
    }
}
